package com.example.myapplication;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.myapplication.fragment.FragAdapter;
import com.example.myapplication.fragment.fragment1;
import com.example.myapplication.fragment.fragment2;
import com.example.myapplication.fragment.fragment3;
import com.example.myapplication.fragment.fragment4;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity  implements View.OnClickListener {
    ViewPager viewPager;
    ImageView img1;
    ImageView img2;
    ImageView img3;
    ImageView img4;
    TextView tv1;
    TextView tv2;
    TextView tv3;
    TextView tv4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);
         viewPager = findViewById(R.id.view_pager);

        List<Fragment> list = new ArrayList<>();
        fragment1 fragment1 = new fragment1();
        list.add(fragment1);


        fragment2 fragment2 = new fragment2();
        list.add(fragment2);

        fragment3 fragment3 = new fragment3();
        list.add(fragment3);

        fragment4 fragment4 = new fragment4();
        list.add(fragment4);

        FragAdapter adapter = new FragAdapter(getSupportFragmentManager(),list);
        viewPager.setAdapter(adapter);

        LinearLayout tab1 = findViewById(R.id.tab1);
        tab1.setOnClickListener(this);
        LinearLayout tab2 = findViewById(R.id.tab2);
        tab2.setOnClickListener(this);
        LinearLayout tab3 = findViewById(R.id.tab3);
        tab3.setOnClickListener(this);
        LinearLayout tab4 = findViewById(R.id.tab4);
        tab4.setOnClickListener(this);
         img1 = findViewById(R.id.img1);
         img2 = findViewById(R.id.img2);
         img3 = findViewById(R.id.img3);
         img4 = findViewById(R.id.img4);
         tv1 = findViewById(R.id.tv1);
         tv2 = findViewById(R.id.tv2);
         tv3 = findViewById(R.id.tv3);
         tv4 = findViewById(R.id.tv4);


        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setNormal();
                if (position == 0){
                    img1.setBackgroundResource(R.drawable.home_page_tab1_selected);
                    tv1.setTextColor(getResources().getColor(R.color.yellow));
                }else if (position == 1 ){
                    img2.setBackgroundResource(R.drawable.home_page_tab2_selected);
                    tv2.setTextColor(getResources().getColor(R.color.yellow));
                }else if (position == 2 ){
                    img3.setBackgroundResource(R.drawable.home_page_tab3_selected);
                    tv3.setTextColor(getResources().getColor(R.color.yellow));
                }else if (position == 3 ){
                    img4.setBackgroundResource(R.drawable.home_page_tab4_selected);
                    tv4.setTextColor(getResources().getColor(R.color.yellow));
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        img1.setBackgroundResource(R.drawable.home_page_tab1_selected);
        tv1.setTextColor(getResources().getColor(R.color.gary));

    }


    @RequiresApi(api = Build.VERSION_CODES.M)
    @SuppressLint("ResourceType")
    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.tab1:
                viewPager.setCurrentItem(0);
                img1.setBackgroundResource(R.drawable.home_page_tab1_selected);
                tv1.setTextColor(getResources().getColor(R.color.yellow));
                break;
            case R.id.tab2:
                viewPager.setCurrentItem(1);
                img2.setBackgroundResource(R.drawable.home_page_tab2_selected);
                tv2.setTextColor(getResources().getColor(R.color.yellow));
                break;
            case R.id.tab3:
                viewPager.setCurrentItem(2);
                img3.setBackgroundResource(R.drawable.home_page_tab3_selected);
                tv3.setTextColor(getResources().getColor(R.color.yellow));
                break;
            case R.id.tab4:
                viewPager.setCurrentItem(4);
                img4.setBackgroundResource(R.drawable.home_page_tab4_selected);
                tv4.setTextColor(getResources().getColor(R.color.yellow));
                break;
        }

    }
    private void setNormal() {
        img1.setBackgroundResource(R.drawable.home_page_tab1_normal);
        img2.setBackgroundResource(R.drawable.home_page_tab2_normal);
        img3.setBackgroundResource(R.drawable.home_page_tab3_normal);
        img4.setBackgroundResource(R.drawable.home_page_tab4_normal);
        tv1.setTextColor(getResources().getColor(R.color.gary));
        tv2.setTextColor(getResources().getColor(R.color.gary));
        tv3.setTextColor(getResources().getColor(R.color.gary));
        tv4.setTextColor(getResources().getColor(R.color.gary));
    }
}

